package com.ywj.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class KafkaController {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    /**
     * 接收POST请求并将消息发送到Kafka
     * @param message
     */
    @PostMapping("/send")
    public void sendMessage(@RequestBody String message){
        log.info("Producer send message : [{}]", message);
        //发送消息到my-topic主题，send方法返回一个ListenableFuture对象 用于异步处理结果
        kafkaTemplate.send("my-topic", message);
    }
}
